package com.github.yoojia.mqtt;

/**
 * @author Yoojia Chen (yoojiachen@gmail.com)
 * @since 1.0.0
 */
public class Message {

    /**
     * 所处服务的名称
     */
    public final String domain;

    /**
     * 消息的目标节点ID
     */
    public final String targetNodeId;

    /**
     * 消息类型
     */
    public final String type;

    /**
     * 消息流水ID
     */
    public final long messageId;

    /**
     * 消息发送者的节点ID
     */
    public final String senderNodeId;

    /**
     * 消息标签，用于扩展；
     */
    public final String tag;

    /**
     * 消息负载
     */
    public final byte[] payload;

    /**
     * MQTT的QOS值
     */
    public final int qos;

    /**
     * MQTT的retained值
     */
    public final boolean retained;

    private Message(String domain, String targetNodeId, String type, long messageId, String senderNodeId, String tag,
                    byte[] payload,
                    int qos, boolean retained) {
        this.domain = domain;
        this.targetNodeId = targetNodeId;
        this.type = type;
        this.messageId = messageId;
        this.senderNodeId = senderNodeId;
        this.tag = tag;
        this.payload = payload;
        this.qos = qos;
        this.retained = retained;
    }

    /**
     * 获取此消息的Builder对象
     * @return Builder对象
     */
    public Builder builder(){
        return new Builder()
                .domain(domain)
                .targetNodeId(targetNodeId)
                .type(type)
                .messageId(messageId)
                .senderNodeId(senderNodeId)
                .tag(tag)
                .payload(payload)
                .qos(qos)
                .retained(retained);
    }

    /**
     * 获取字符串的Payload数据
     * @return Payload的字符串类型数据
     */
    public String getStringPayload(){
        return new String(payload);
    }

    @Override
    public String toString() {
        return LimitedString.toString(512, payload);
    }

    public static class Builder {

        private boolean mRetained = false;
        private int mQos = 2;

        private String mDomain;
        private String mTargetNodeId;
        private String mType;
        private long mMessageId;
        private String mSenderNodeId;
        private String mTag;
        private byte[] mPayloads;

        public Builder retained(boolean retained){
            mRetained = retained;
            return this;
        }

        public Builder qos(int qos){
            mQos = qos;
            return this;
        }

        public Builder domain(String domain){
            mDomain = domain;
            return this;
        }

        public Builder targetNodeId(String nodeId){
            mTargetNodeId = nodeId;
            return this;
        }

        public Builder type(String type){
            mType = type;
            return this;
        }

        public Builder messageId(long messageId) {
            mMessageId = messageId;
            return this;
        }

        public Builder senderNodeId(String nodeId) {
            mSenderNodeId = nodeId;
            return this;
        }

        public Builder tag(String tag) {
            mTag = tag;
            return this;
        }

        public Builder payload(byte[] payloads){
            mPayloads = payloads;
            return this;
        }

        public Message build(){
            return new Message(mDomain, mTargetNodeId, mType, mMessageId, mSenderNodeId, mTag,
                    mPayloads,
                    mQos, mRetained);
        }
    }

}
